package com.zhx.real.advice.advice;

import com.zhx.real.advice.exception.CheckException;
import com.zhx.real.advice.exception.MyRuntimeException;
import com.zhx.real.core.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-03-21 10:33
 * @description：用途: 全局异常处理
 * @modified By：zhanghaixuan
 * @version: 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {
    @ExceptionHandler(value = CheckException.class)
    public Result<String> handlerAdException(HttpServletRequest request, CheckException exception) {
        log.error("参数异常:",exception.getMessage());
        Result<String> commonResponse = new Result<String>(-1,exception.getMessage());
        commonResponse.setData(exception.getMessage());
        return commonResponse;
    }

    @ExceptionHandler(value = MyRuntimeException.class)
    public Result<String> handlerAdException(HttpServletRequest request, MyRuntimeException exception) {
        log.error("运行异常:",exception.getMessage());
        Result<String> commonResponse = new Result<String>(500,exception.getMessage());
        commonResponse.setData(exception.getMessage());
        return commonResponse;
    }

    @ExceptionHandler(value = Exception.class)
    public Result<String> handlerAdException(HttpServletRequest request, Exception exception) {
        log.error("未捕获的异常种类:",exception.getMessage());
        Result<String> commonResponse = new Result<String>(-1,exception.getMessage());
        commonResponse.setData(exception.getMessage());
        return commonResponse;
    }

}
